package com.ymk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ymk.constant.MessageConstant;
import com.ymk.pojo.OrderSetting;
import com.ymk.service.OrderSettingService;
import com.ymk.util.POIUtils;
import com.ymk.vo.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.web.messaging.MessageSecurityMetadataSourceRegistry;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            //使用poi工具类解析excel文件，读取里面的内容
            List<String[]> lists = POIUtils.readExcel(excelFile);
            //把List<String[]>数据转换成List<OrderSetting>数据
            List<OrderSetting> orderSettings = new ArrayList<>();
            //迭代里面的每一行数据，进行封装到集合里面
            for (String[] str : lists) {
                //获取到一行里面，每个表格数据，进行封装
                OrderSetting orderSetting = new OrderSetting
                        (new Date(str[0]),Integer.parseInt(str[1]));
                orderSettings.add(orderSetting);
            }
            //调用业务进行保存
            orderSettingService.add(orderSettings);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据日期查询预约设置数据（获取指定日期所在月份的预约设置数据）
     * @param date
     * @return
     */
    @RequestMapping("/getOrderSettingByMonth")
    @PreAuthorize("hasAuthority('ORDERSETTING')")//权限校验
    public Result getOrderSettingByMonth(String date){//参数格式为：
        try{
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            //获取预约设置数据成功
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //获取预约设置数据失败
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/editNumberByDate")
    @PreAuthorize("hasAuthority('ORDERSETTING')")//权限校验
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try{
            orderSettingService.editNumberByDate(orderSetting);
            //预约设置成功
            return new Result(true,MessageConstant.ORDER_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            //预约设置失败
            return new Result(false, MessageConstant.ORDER_FAIL);
        }
    }


}
